import { observable, action } from "mobx"
import { getUserId } from '@/utils/utils';
import { network } from "@/utils/network"
import { ORDER_TAB_STATUS } from '@/utils/constants'
import Taro from '@tarojs/taro'

/**
 * 我的model
 */
class MeModel {

  // @observable.ref
  // shopDetail: any


  @observable.ref
  userInfo: any


  @observable.ref
  orderStatus: Array<any>

  @observable.ref
  iconList: Array<String>

  @observable.ref
  statusNumber: Array<number>

  @observable
  isLogin: boolean

  constructor() {
    this.userInfo = {}
    this.statusNumber = []
    this.isLogin = false;
    this.iconList = [
      'http://xmmi.oss-cn-beijing.aliyuncs.com/icon/3857.png',
      'http://xmmi.oss-cn-beijing.aliyuncs.com/icon/3856.png',
      'http://xmmi.oss-cn-beijing.aliyuncs.com/icon/3854.png',
      'http://xmmi.oss-cn-beijing.aliyuncs.com/icon/3855.png',
    ];
    this.orderStatus = Object.keys(ORDER_TAB_STATUS)
      .map((key: string, index: number) => ({
        status: key,
        name: this.iconList[index],
        text: ORDER_TAB_STATUS[key]
      }))
  }

  @action
  setUserInfo(detail) {
    this.userInfo = detail
  }

  getData() {
    this.isLogin = !!Taro.getStorageSync('authorization');
    if (this.isLogin) {
      network.get('/xxm/user/info').then((res) => {
        this.setUserInfo(res.data)
      });
      network.post('/xxm/order/number', {
        user_id: getUserId()
      }).then((res) => {
        this.statusNumber = res.data || [];
      });
    }
  }

}

export const meModel = new MeModel()